﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using XRERP.Models;
using XRERP.utils;

namespace XRERP.Controllers
{
    /// <summary>
    /// 人工费用录入
    /// </summary>
    public class LaborInputController : Controller
    {

        private MySqlDB db = new MySqlDB();

        // GET: LaborInput
        public ActionResult Index()
        {
            return View();
        }

        /// <summary>
        /// 保存人工费信息
        /// </summary>
        /// <param name="laborcost"></param>
        /// <returns></returns>
        public ActionResult SaveLaborInput(xr_bl_laborcost laborcost)
        {
            //xr_bl_devconfigmain info = new xr_bl_devconfigmain();
            JRow<xr_bl_laborcost> json = new JRow<xr_bl_laborcost>();


            var probill=db.xr_bl_probillmain.Where(x => x.probillmain_uuid==laborcost.probillmain_uuid);

            if(null==probill)
            {
                json.isError = false;
                json.code = 300;
                json.msg = "保存失败，未找到对应的请款单信息";

            }


            try
            {
                laborcost.laborcost_uuid= Tools.getGuid();
                laborcost.validflag = "1";

                db.xr_bl_laborcost.Add(laborcost);
                if(db.SaveChanges()==1)
                {
                    json.isError = false;
                    json.code = 200;
                    json.msg = "保存成功!";
                    json.RowData = laborcost;
                }
                else
                {
                    json.isError = true;
                    json.code = 300;
                    json.msg = "保存失败，请重试或与管理员联系！";
                }
               
            }
            catch (Exception e)
            {
                json.code = 505;
                json.isError = true;
                json.msg = e.ToString();
            }

            return Json(json);
        }

        /// <summary>
        /// 查询人工费信息
        /// </summary>
        /// <param name="page"></param>
        /// <param name="rows"></param>
        /// <returns></returns>
        public ActionResult QueryLaborInput(String probillmain_uuid = "",int page = 1, int rows = 10)
        {
            Euitable<xr_bl_laborcost> table = new Euitable<xr_bl_laborcost>();
            //设置查询条件
            Func<xr_bl_laborcost, bool> where = x => x.validflag == "1";
            Func<xr_bl_laborcost, bool> where2 = x => x.probillmain_uuid == probillmain_uuid;
            table.rows = db.xr_bl_laborcost
                .Where(where).Where(where2)
                .OrderBy(y => y.release_month)
                .Skip<xr_bl_laborcost>(rows * (page - 1))
                .Take<xr_bl_laborcost>(rows)
                .ToList<xr_bl_laborcost>();
            /*table.total = db.xr_bl_laborcost
                .Where(where).Where(where2)
                .Count();*/
            return Json(table.rows);
        }
    }
}